/*
 * Copyright 2005 MH-Software-Entwicklung. All rights reserved.
 * Use is subject to license terms.
 */

package com.bnc.comps;

import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import com.bnc.images.ImageHelper;
import com.bnc.impl.IBNCDesk;
import com.bnc.props.MenuProperties;

/**
 * @author Michael Hagen
 */
public class MainToolBar extends JToolBar implements ActionListener
{
	private Component parent = null;
	private static IBNCDesk app = null;
	
    private ImageIcon btnImage[] = new ImageIcon[MenuProperties.MENU_LABEL.length];
    private ToolButton toolButton[] = new ToolButton[MenuProperties.MENU_LABEL.length];
    
    public MainToolBar( Component aParent ) {
        super();
        
        parent = aParent;
        app = (IBNCDesk)parent;
        
        setFloatable( true );
        setMargin( new Insets( 2, 0, 2, 0 ) );
        
        initElement();
    }
    
    private void initElement()
    {
    	int menuLen = toolButton.length;
        for ( int i=0; i<menuLen; i++ )
        {
        	btnImage[i] = ImageHelper.loadImage( MenuProperties.MENU_ICON[i] );
        	toolButton[i] = new ToolButton( btnImage[i] );
        	toolButton[i].setToolTipText( MenuProperties.MENU_LABEL[i] );
        	toolButton[i].addActionListener( this );
        	
        	add( toolButton[i] );
        }
    }
    
    public void actionPerformed( ActionEvent e ) 
    {
    	JButton btn = (JButton)e.getSource();
    	String title = btn.getToolTipText().trim();
    	
    	int idx = 0;
    	int len = MenuProperties.MENU_LABEL.length;
    	for ( int i=0; i<len; i++ )
    	{
    		if ( MenuProperties.MENU_LABEL[i].equals( title ) )
    		{
    			idx = i;
    			break;
    		}
    	}
    	
    	app.getInternalFramePanel().setInternalFrame( MenuProperties.MENU_LABEL[idx] );
    }
    
    private class ToolButton extends JButton {
        
        public ToolButton(Icon icon) 
        {
            super(icon);
            setMargin(new Insets(4, 4, 4, 4));
        }
        
        public boolean isFocusTraversable() 
        { 
            return false; 
        }
        
        public void requestFocus() {}
    }
    
    private class ToogleToolButton extends JToggleButton 
    {
        
        public ToogleToolButton(Icon icon) 
        {
            super(icon);
            setMargin(new Insets(4, 4, 4, 4));
        }
        
        public boolean isFocusTraversable() 
        { 
            return false; 
        }
        
        public void requestFocus() {}
    }
}
